'use strict';

const Controller = require('./base');

class CategoryController extends Controller {
  async getList() {
    try {
      const { ctx } = this;

      const result = {};
      const hits = await ctx.service.category.selectOrder();
      hits.forEach(item => {
        const body = item._source;
        if (body.parent_id === '0') {
          result[body.id] = { ...body, children: [] };
        } else {
          result[body.parent_id].children.push(body);
        }
      });
      this.success(Object.values(result));
    } catch (err) {
      this.error(`获取类型列表失败，${err}`);
    }
  }
}

module.exports = CategoryController;
